package com.yuexunit.finance.project.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.project.entity.BailReturnEntity;
import com.yuexunit.finance.project.entity.lexicon.AgreeStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;


/**
 * @author wenwenlong
 */
@Implements(BailReturnService.class)
public class BailReturnServiceImpl implements BailReturnService{

	@Override
	@Transactional
	public Long createBailReturnInfo(BailReturnEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getBailReturnInfoId();
	}

	@Override
	@Transactional
	public void updateBailReturnInfo(BailReturnEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void deleteBailReturnInfo(Long bailReturnInfoId) {
		PersistUtil.delete(BailReturnEntity.class, bailReturnInfoId);
	}

	@Override
	public BailReturnEntity inquireBailReturnInfo(Long bailReturnInfoId) {
		BailReturnEntity entity = PersistUtil.get(BailReturnEntity.class, bailReturnInfoId);
		return entity;
	}

	@Override
	public PageList<BailReturnEntity> inquireBailReturnInfoPageList(String financeNumber, PageCriteria page) {
		DetachedCriteria criterial = DetachedCriteria.forClass(BailReturnEntity.class);
		if (StringUtil.isNotBlank(financeNumber)) {
			criterial.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		criterial.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criterial, page);
	}

	@Override
	public List<Long> countBailReturnAudits() {
		DetachedCriteria criteria = DetachedCriteria.forClass(BailReturnEntity.class);
		criteria.add(Restrictions.eq("accountingAuditState", AgreeStateEnum.UNKNOWN));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("bailReturnInfoId")));
		return PersistUtil.get(criteria);
	}
}
